#include "Scene_PCH.h"
#include "bInfinitePlane.h"

/*--------------------------------------------------------------------------*/
const RMaterial* mat = NULL;

bInfinitePlane::bInfinitePlane(btDiscreteDynamicsWorld* world, float constant, const bInitData& initdata) :
	bObject(world)
{
	m_constant = constant;
	m_staticPlaneShape = new btStaticPlaneShape(btVector3(0.0f, 1.0f, 0.0f), constant);
	AddRigidBody(m_staticPlaneShape, initdata);

	m_material = NULL;
}

/*--------------------------------------------------------------------------*/

bInfinitePlane::~bInfinitePlane()
{
	RemoveRigidBody();
	delete m_staticPlaneShape;
}

/*--------------------------------------------------------------------------*/

void bInfinitePlane::PreRendering( uint arg )
{
	if (m_material == NULL)
	{
		MaterialDesc md;
		md.m_flags = EMaterialDesc::ColorAsTexture0| EMaterialDesc::OutNormal | EMaterialDesc::UseBumpmapping;
		md.m_blendstate = g_Renderer->GetRendererStorageMgr()->GetBlendState("terrain");
		md.m_rasterizerstate = g_Renderer->GetRendererStorageMgr()->GetRasterizerState("terrain");
		md.m_depthstencilstate = g_Renderer->GetRendererStorageMgr()->GetDepthstencilState("terrain");
		md.m_color = glm::vec4(1.0f, 1.0f, 1.0f, 1.0f);
		md.m_Bumpmapping_normal0 = g_Renderer->GetRendererStorageMgr()->GetTexture("normal");
		md.m_Bumpmapping_TextureMultiplier = 200.0f;
		md.m_texture0 = g_Renderer->GetRendererStorageMgr()->GetTexture("sand1");

		m_material = g_Renderer->GetMaterialMgr()->GetMaterialFromMaster(md);
	}

	switch (arg)
	{
	case 0:
		{
			float infinity = 10000.0f;

			DynamicSurface* ds = g_Renderer->GetDynGeometryMgr()->BeginGeometry(6, EVertex::Position0 | EVertex::Normal0 | EVertex::TexCoord0);

			ds->vPosition0()[0] = glm::vec3(-infinity, m_constant, -infinity);
			ds->vNormal0()[0] = glm::vec3(0.0f, 1.0f, 0.0f);
			ds->vTexcoord0()[0] = glm::vec2(0.0f, 0.0f);
			
			ds->vPosition0()[1] = glm::vec3(infinity, m_constant, infinity);
			ds->vNormal0()[1] = glm::vec3(0.0f, 1.0f, 0.0f);
			ds->vTexcoord0()[1] = glm::vec2(1.0f, 1.0f);

			ds->vPosition0()[2] = glm::vec3(infinity, m_constant, -infinity);
			ds->vNormal0()[2] = glm::vec3(0.0f, 1.0f, 0.0f);
			ds->vTexcoord0()[2] = glm::vec2(1.0f, 0.0f);

			ds->vPosition0()[3] = glm::vec3(-infinity, m_constant, -infinity);
			ds->vNormal0()[3] = glm::vec3(0.0f, 1.0f, 0.0f);
			ds->vTexcoord0()[3] = glm::vec2(0.0f, 0.0f);

			ds->vPosition0()[4] = glm::vec3(-infinity, m_constant, infinity);
			ds->vNormal0()[4] = glm::vec3(0.0f, 1.0f, 0.0f);
			ds->vTexcoord0()[4] = glm::vec2(0.0f, 1.0f);

			ds->vPosition0()[5] = glm::vec3(infinity, m_constant, infinity);
			ds->vNormal0()[5] = glm::vec3(0.0f, 1.0f, 0.0f);
			ds->vTexcoord0()[5] = glm::vec2(1.0f, 1.0f);

			g_Renderer->GetQueueMgr()->AddTask(this, m_material, g_Renderer->GetDynGeometryMgr()->EndGeometry(), ETopology::Triangles, 6, ETask::DynamicVAO);
			break;
		}
	}
}

/*--------------------------------------------------------------------------*/

void bInfinitePlane::WriteModelMatrix( float* modelmatrix ) const
{
	btTransform transform;
	m_rigidBody->getMotionState()->getWorldTransform(transform);
	transform.getOpenGLMatrix(modelmatrix);
}

/*--------------------------------------------------------------------------*/
